package dao;

import java.util.List;

import idao.IOrdersDAO;
import model.Orders;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrdersDAO implements IOrdersDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public OrdersDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean insert(Orders orders) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(orders);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	@Override
	public boolean update(Orders orders) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(orders);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	@Override
	public boolean delete(Orders orders) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(orders);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	@Override
	public Orders getOrdersByID(int id) {
		// TODO Auto-generated method stub
		try {
			Orders orders = (Orders) sessionFactory.getCurrentSession().get(
					Orders.class, id);
			return orders;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Orders> getAll() {
		// TODO Auto-generated method stub
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(
					"from Orders");
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
